home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / info / ccmode-2.z / ccmode-2 (.txt)
GNU Info File  |  1998-10-27  |  49KB  |  909 lines

  1. This is Info file ../info/ccmode, produced by Makeinfo-1.63 from the
  2. input file cc-mode.texi.
  3.    Copyright (C) 1995 Free Software Foundation, Inc.
  4. File: ccmode,  Node: Custom Indentation Functions,  Next: Custom Brace and Colon Hanging,  Up: Advanced Customizations
  5. Custom Indentation Functions
  6. ----------------------------
  7.    One of the most common ways to customize `cc-mode' is by writing
  8. "custom indentation functions" and associating them with specific
  9. syntactic symbols (see *Note Syntactic Symbols::).  `cc-mode' itself
  10. uses custom indentation functions to provide more sophisticated
  11. indentation, for example when lining up C++ stream operator blocks:
  12.      1: void main(int argc, char**)
  13.      2: {
  14.      3:   cout << "There were "
  15.      4:     << argc
  16.      5:     << "arguments passed to the program"
  17.      6:     << endl;
  18.      7: }
  19.    In this example, lines 4 through 6 are assigned the `stream-op'
  20. syntactic symbol.  If `stream-op' had an offset of `+', and
  21. `c-basic-offset' was 2, lines 4 through 6 would simply be indented two
  22. spaces to the right of line 3.  But perhaps we'd like `cc-mode' to be a
  23. little more intelligent so that it offsets the stream operators under
  24. the operator in line 3.  To do this, we have to write a custom
  25. indentation function which finds the column of first stream operator on
  26. the first line of the statement.  Here is the lisp code (from the
  27. `cc-mode.el' source file) that implements this:
  28.      (defun c-lineup-streamop (langelem)
  29.        ;; lineup stream operators
  30.        (save-excursion
  31.          (let* ((relpos (cdr langelem))
  32.                 (curcol (progn (goto-char relpos)
  33.                                (current-column))))
  34.            (re-search-forward "<<\\|>>" (c-point 'eol) 'move)
  35.            (goto-char (match-beginning 0))
  36.            (- (current-column) curcol))))
  37. Custom indent functions take a single argument, which is a syntactic
  38. component cons cell (see *Note Syntactic Analysis::).  The function
  39. returns an integer offset value that will be added to the running total
  40. indentation for the line.  Note that what actually gets returned is the
  41. difference between the column that the first stream operator is on, and
  42. the column of the buffer relative position passed in the function's
  43. argument.  Remember that `cc-mode' automatically adds in the column of
  44. the component's relative buffer position and we don't want that value
  45. added into the final total twice.
  46.    Now, to associate the function `c-lineup-streamop' with the
  47. `stream-op' syntactic symbol, we can add something like the following
  48. to our `c++-mode-hook'(1):
  49.      (c-set-offset 'stream-op 'c-lineup-streamop)
  50.    Now the function looks like this after re-indenting (using `C-c
  51. C-q'):
  52.      1: void main(int argc, char**)
  53.      2: {
  54.      3:   cout << "There were "
  55.      4:        << argc
  56.      5:        << "arguments passed to the program"
  57.      6:        << endl;
  58.      7: }
  59.    Custom indentation functions can be as simple or as complex as you
  60. like, and any syntactic symbol that appears in `c-offsets-alist' can
  61. have a custom indentation function associated with it.
  62.    ---------- Footnotes ----------
  63.    (1)  It probably makes more sense to add this to `c++-mode-hook'
  64. than `c-mode-common-hook' since stream operators are only relevant for
  65. File: ccmode,  Node: Custom Brace and Colon Hanging,  Next: Customizing Semi-colons and Commas,  Prev: Custom Indentation Functions,  Up: Advanced Customizations
  66. Custom Brace and Colon Hanging
  67. ------------------------------
  68.    Syntactic symbols aren't the only place where you can customize
  69. `cc-mode' with the lisp equivalent of callback functions.  Brace
  70. hanginess can also be determined by custom functions associated with
  71. syntactic symbols on the `c-hanging-braces-alist' variable.  Remember
  72. that ACTION's are typically a list containing some combination of the
  73. symbols `before' and `after' (see *Note Hanging Braces::).  However, an
  74. ACTION can also be a function symbol which gets called when a brace
  75. matching that syntactic symbol is typed.
  76.    These ACTION functions are called with two arguments: the syntactic
  77. symbol for the brace, and the buffer position at which the brace was
  78. inserted.  The ACTION function is expected to return a list containing
  79. some combination of `before' and `after'.  The function can also return
  80. `nil'.  This return value has the normal brace hanging semantics
  81. described in *Note Hanging Braces::.
  82.    As an example, `cc-mode' itself uses this feature to dynamically
  83. determine the hanginess of braces which close `do-while' constructs:
  84.      void do_list( int count, char** atleast_one_string )
  85.      {
  86.          int i=0;
  87.          do {
  88.              handle_string( atleast_one_string( i ));
  89.              i++;
  90.          } while( i < count );
  91.      }
  92.    `cc-mode' assigns the `block-close' syntactic symbol to the brace
  93. that closes the `do' construct, and normally we'd like the line that
  94. follows a `block-close' brace to begin on a separate line.  However,
  95. with `do-while' constructs, we want the `while' clause to follow the
  96. closing brace.  To do this, we associate the `block-close' symbol with
  97. the ACTION function `c-snug-do-while':
  98.      (defun c-snug-do-while (syntax pos)
  99.        "Dynamically calculate brace hanginess for do-while statements.
  100.      Using this function, `while' clauses that end a `do-while' block will
  101.      remain on the same line as the brace that closes that block.
  102.      
  103.      See `c-hanging-braces-alist' for how to utilize this function as an
  104.      ACTION associated with `block-close' syntax."
  105.        (save-excursion
  106.          (let (langelem)
  107.            (if (and (eq syntax 'block-close)
  108.                     (setq langelem (assq 'block-close c-syntactic-context))
  109.                     (progn (goto-char (cdr langelem))
  110.                            (if (= (following-char) ?{)
  111.                                (forward-sexp -1))
  112.                            (looking-at "\\<do\\>[^_]")))
  113.                '(before)
  114.              '(before after)))))
  115.    This function simply looks to see if the brace closes a `do-while'
  116. clause and if so, returns the list ``(before)'' indicating that a
  117. newline should be inserted before the brace, but not after it.  In all
  118. other cases, it returns the list ``(before after)'' so that the brace
  119. appears on a line by itself.
  120.    During the call to the brace hanging ACTION function, the variable
  121. `c-syntactic-context' is bound to the full syntactic analysis list.
  122.    Note that for symmetry, colon hanginess should be customizable by
  123. allowing function symbols as ACTIONs on the `c-hanging-colon-alist'
  124. variable.  Since no use has actually been found for this feature, it
  125. isn't currently implemented.
  126. File: ccmode,  Node: Customizing Semi-colons and Commas,  Next: Other Special Indentations,  Prev: Custom Brace and Colon Hanging,  Up: Advanced Customizations
  127. Customizing Semi-colons and Commas
  128. ----------------------------------
  129.    You can also customize the insertion of newlines after semi-colons
  130. and commas, when the auto-newline minor mode is enabled (see *Note
  131. Minor Modes::).  This is controlled by the variable
  132. `c-hanging-semi&comma-criteria', which contains a list of functions
  133. that are called in the order they appear.  Each function is called with
  134. zero arguments, and is expected to return one of the following values:
  135.    * non-`nil' - A newline is inserted, and no more functions from the
  136.      list are called.
  137.    * `stop' - No more functions from the list are called, but no
  138.      newline is inserted.
  139.    * `nil' - No determination is made, and the next function in the
  140.      list is called.
  141.    If every function in the list is called without a determination being
  142. made, then no newline is added. The default value for this variable is a
  143. list containing a single function which inserts newlines only after
  144. semi-colons which do not appear inside parenthesis lists (i.e. those
  145. that separate `for'-clause statements).
  146. File: ccmode,  Node: Other Special Indentations,  Prev: Customizing Semi-colons and Commas,  Up: Advanced Customizations
  147. Other Special Indentations
  148. --------------------------
  149.    One other customization variable is available in `cc-mode':
  150. `c-special-indent-hook'.  This is a standard hook variable that is
  151. called after every line is indented by `cc-mode'.  You can use it to do
  152. any special indentation or line adjustments your style dictates, such
  153. as adding extra indentation to constructors or destructor declarations
  154. in a class definition, etc.  Note however, that you should not change
  155. point or mark inside your `c-special-indent-hook' functions (i.e.
  156. you'll probably want to wrap your function in a `save-excursion').
  157. File: ccmode,  Node: Syntactic Symbols,  Next: Performance Issues,  Prev: Customizing Indentation,  Up: Top
  158. Syntactic Symbols
  159. *****************
  160.    The complete list of recognized syntactic symbols is described in the
  161. `c-offsets-alist' variable.  This chapter will provide some examples to
  162. help clarify these symbols.
  163.    Most syntactic symbol names follow a general naming convention.
  164. When a line begins with an open or close brace, the syntactic symbol
  165. will contain the suffix `-open' or `-close' respectively.
  166.    Usually, a distinction is made between the first line that
  167. introduces a construct and lines that continue a construct, and the
  168. syntactic symbols that represent these lines will contain the suffix
  169. `-intro' or `-cont' respectively.  As a sub-classification of this
  170. scheme, a line which is the first of a particular brace block construct
  171. will contain the suffix `-block-intro'.
  172.    Let's look at some examples to understand how this works.  Remember
  173. that you can check the syntax of any line by using `C-c C-s'.
  174.        1: void
  175.        2: swap( int& a, int& b )
  176.        3: {
  177.        4:     int tmp = a;
  178.        5:     a = b;
  179.        6:     b = tmp;
  180.        7:     int ignored =
  181.        8:         a + b;
  182.        9: }
  183.    Line 1 shows a `topmost-intro' since it is the first line that
  184. introduces a top-level construct.  Line 2 is a continuation of the
  185. top-level construct introduction so it has the syntax
  186. `topmost-intro-cont'.  Line 3 shows a `defun-open' since it is the
  187. brace that opens a top-level function definition.  Line 9 is a
  188. `defun-close' since it contains the brace that closes the top-level
  189. function definition.  Line 4 is a `defun-block-intro', i.e. it is the
  190. first line of a brace-block, which happens to be enclosed in a
  191. top-level function definition.
  192.    Lines 5, 6, and 7 are all given `statement' syntax since there isn't
  193. much special about them.  Note however that line 8 is given
  194. `statement-cont' syntax since it continues the statement begun on the
  195. previous line.
  196.    Here's another example, which illustrates some C++ class syntactic
  197. symbols:
  198.         1: class Bass
  199.         2:     : public Guitar,
  200.         3:       public Amplifiable
  201.         4: {
  202.         5: public:
  203.         6:     Bass()
  204.         7:         : eString( new BassString( 0.105 )),
  205.         8:           aString( new BassString( 0.085 )),
  206.         9:           dString( new BassString( 0.065 )),
  207.        10:           gString( new BassString( 0.045 ))
  208.        11:     {
  209.        12:         eString.tune( 'E' );
  210.        13:         aString.tune( 'A' );
  211.        14:         dString.tune( 'D' );
  212.        15:         gString.tune( 'G' );
  213.        16:     }
  214.        17: }
  215.    As in the previous example, line 1 has the `topmost-intro' syntax.
  216. Here however, the brace that opens a C++ class definition on line 4 is
  217. assigned the `class-open' syntax.  Note that in C++, structs and unions
  218. are essentially equivalent syntactically (and are very similar
  219. semantically), so replacing the `class' keyword in the example above
  220. with `struct' or `union' would still result in a syntax of `class-open'
  221. for line 4 (1).  Similarly, line 17 is assigned `class-close' syntax.
  222.    Line 2 introduces the inheritance list for the class so it is
  223. assigned the `inher-intro' syntax, and line 3, which continues the
  224. inheritance list is given `inher-cont' syntax.
  225.    Things get interesting at line 5.  The primary syntactic symbol for
  226. this line is `access-label' since this a label keyword that specifies
  227. access protection in C++.  However, this line actually shows two
  228. syntactic symbols when you hit `C-c C-s'.  This is because it is also a
  229. top-level construct inside a class definition.  Thus the other
  230. syntactic symbol assigned to this line is `inclass'.  Similarly, line 6
  231. is given both `inclass' and `topmost-intro' syntax.
  232.    Line 7 introduces a C++ member initialization list and as such is
  233. given `member-init-intro' syntax.  Note that in this case it is *not*
  234. assigned `inclass' since this is not considered a top-level construct.
  235. Lines 8 through 10 are all assigned `member-init-cont' since they
  236. continue the member initialization list started on line 7.
  237.    Line 11 is assigned `inline-open' because it opens an "in-class" C++
  238. inline method definition.  This is distinct from, but related to, the
  239. C++ notion of an inline function in that its definition occurs inside
  240. an enclosing class definition, which in C++ implies that the function
  241. should be inlined.  For example, if the definition of the `Bass'
  242. constructor appeared outside the class definition, line 11 would be
  243. given the `defun-open' syntax, even if the keyword `inline' appeared
  244. before the method name, as in:
  245.      class Bass
  246.          : public Guitar,
  247.            public Amplifiable
  248.      {
  249.      public:
  250.          Bass();
  251.      }
  252.      
  253.      inline
  254.      Bass::Bass()
  255.          : eString( new BassString( 0.105 )),
  256.            aString( new BassString( 0.085 )),
  257.            dString( new BassString( 0.065 )),
  258.            gString( new BassString( 0.045 ))
  259.      {
  260.          eString.tune( 'E' );
  261.          aString.tune( 'A' );
  262.          dString.tune( 'D' );
  263.          gString.tune( 'G' );
  264.      }
  265. Similarly, line 16 is given `inline-close' syntax.
  266.    As in the first example above, line 12 is given `defun-block-open'
  267. syntax and lines 13 through 15 are all given `statement' syntax.
  268.    Here is another (totally contrived) example which illustrates how
  269. syntax is assigned to various conditional constructs:
  270.         1: void spam( int index )
  271.         2: {
  272.         3:     for( int i=0; i<index; i++ )
  273.         4:     {
  274.         5:         if( i == 10 )
  275.         6:         {
  276.         7:             do_something_special();
  277.         8:         }
  278.         9:         else
  279.        10:             do_something( i );
  280.        11:     }
  281.        12:     do {
  282.        13:         another_thing( i-- );
  283.        14:     }
  284.        15:     while( i > 0 );
  285.        16: }
  286. Only the lines that illustrate new syntactic symbols will be discussed.
  287.    Line 4 has a brace which opens a conditional's substatement block.
  288. It is thus assigned `substatement-open' syntax, and since line 5 is the
  289. first line in the substatement block, it is assigned
  290. `substatement-block-intro' syntax.  Lines 6 and 7 are assigned similar
  291. syntax.  Line 8 contains the brace that closes the inner substatement
  292. block.  It is given the generic syntax `block-close', as are lines 11
  293. and 14.
  294.    Line 9 is a little different - since it contains the keyword `else'
  295. matching the `if' statement introduced on line 5; it is given the
  296. `else-clause' syntax.  Note also that line 10 is slightly different
  297. too.  Because `else' is considered a conditional introducing keyword
  298. (2), and because the following substatement is not a brace block, line
  299. 10 is assigned the `substatement' syntax.
  300.    One other difference is seen on line 15.  The `while' construct that
  301. closes a `do' conditional is given the special syntax
  302. `do-while-closure' if it appears on a line by itself.  Note that if the
  303. `while' appeared on the same line as the preceding close brace, that
  304. line would have been assigned `block-close' syntax instead.
  305.    Switch statements have their own set of syntactic symbols.  Here's an
  306. example:
  307.         1: void spam( enum Ingredient i )
  308.         2: {
  309.         3:     switch( i ) {
  310.         4:     case Ham:
  311.         5:         be_a_pig();
  312.         6:         break;
  313.         7:     case Salt:
  314.         8:         drink_some_water();
  315.         9:         break;
  316.        10:     default:
  317.        11:         {
  318.        12:             what_is_it();
  319.        13:             break;
  320.        14:         }
  321.        15:     }
  322.        14: }
  323.    Here, lines 4, 7, and 10 are all assigned `case-label' syntax, while
  324. lines 5 and 8 are assigned `statement-case-intro'.  Line 11 is treated
  325. slightly differently since it contains a brace that opens a block - it
  326. is given `statement-case-open' syntax.
  327.    There are a set of syntactic symbols that are used to recognize
  328. constructs inside of brace lists.  A brace list is defined as an `enum'
  329. or aggregate initializer list, such as might statically initialize an
  330. array of structs.  For example:
  331.        1: static char* ingredients[] =
  332.        2: {
  333.        3:     "Ham",
  334.        4:     "Salt",
  335.        5:     NULL
  336.        6: }
  337.    Following convention, line 2 in this example is assigned
  338. `brace-list-open' syntax, and line 3 is assigned `brace-list-intro'
  339. syntax.  Likewise, line 6 is assigned `brace-list-close' syntax.  Lines
  340. 4 and 5 however, are assigned `brace-list-entry' syntax, as would all
  341. subsequent lines in this initializer list.
  342.    A number of syntactic symbols are associated with parenthesis lists,
  343. a.k.a argument lists, as found in function declarations and function
  344. calls.  This example illustrates these:
  345.         1: void a_function( int line1,
  346.         2:                  int line2 );
  347.         3:
  348.         4: void a_longer_function(
  349.         5:     int line1,
  350.         6:     int line2
  351.         7:     );
  352.         8:
  353.         9: void call_them( int line1, int line2 )
  354.        10: {
  355.        11:     a_function(
  356.        12:         line1,
  357.        13:         line2
  358.        14:         );
  359.        15:
  360.        16:     a_longer_function( line1,
  361.        17:                        line2 );
  362.        18: }
  363.    Lines 5 and 12 are assigned `arglist-intro' syntax since they are
  364. the first line following the open parenthesis, and lines 7 and 14 are
  365. assigned `arglist-close' syntax since they contain the parenthesis that
  366. closes the argument list.
  367.    The other lines with relevant syntactic symbols include lines 2 and
  368. 17 which are assigned `arglist-cont-nonempty' syntax.  What this means
  369. is that they continue an argument list, but that the line containing the
  370. parenthesis that opens the list is *non-empty* following the open
  371. parenthesis.  Contrast this against lines 6 and 13 which are assigned
  372. `arglist-cont' syntax.  This is because the parenthesis that opens
  373. their argument lists is the last character on that line (3).
  374.    Note that there is no `arglist-open' syntax.  This is because any
  375. parenthesis that opens an argument list, appearing on a separate line,
  376. is assigned the `statement-cont' syntax instead.
  377.    A few miscellaneous syntactic symbols that haven't been previously
  378. covered are illustrated by this example:
  379.         1: void Bass::play( int volume )
  380.         2: const
  381.         3: {
  382.         4:     /* this line starts a multi-line
  383.         5:      * comment.  This line should get `c' syntax */
  384.         6:
  385.         7:     char* a_long_multiline_string = "This line starts a multi-line \
  386.         8: string.  This line should get `string' syntax.";
  387.         9:
  388.        10:   note:
  389.        11:     {
  390.        12: #ifdef LOCK
  391.        13:         Lock acquire();
  392.        14: #endif // LOCK
  393.        15:         slap_pop();
  394.        16:         cout << "I played "
  395.        17:              << "a note\n";
  396.        18:     }
  397.        19: }
  398.    The lines to note in this example include:
  399.    * line 2 which is assigned the `ansi-funcdecl-cont' syntax;
  400.    * line 4 which is assigned both `defun-block-intro' *and*
  401.      `comment-intro' syntax (4);
  402.    * line 5 which is assigned `c' syntax;
  403.    * line 6 which, even though it contains nothing but whitespace, is
  404.      assigned `defun-block-intro'.  Note that the appearance of the
  405.      comment on lines 4 and 5 do not cause line 6 to be assigned
  406.      `statement' syntax because comments are considered to be
  407.      "syntactic whitespace", which are essentially ignored when
  408.      analyzing code;
  409.    * line 8 which is assigned `string' syntax;
  410.    * line 10 which is assigned `label' syntax;
  411.    * line 11 which is assigned `block-open' syntax;
  412.    * lines 12 and 14 which are assigned `cpp-macro' syntax;
  413.    * line 17 which is assigned `stream-op' syntax (5).
  414.    In Objective-C buffers, there are three additional syntactic symbols
  415. assigned to various message calling constructs.  Here's an example
  416. illustrating these:
  417.        1: - (void)setDelegate:anObject
  418.        2:           withStuff:stuff
  419.        3: {
  420.        4:     [delegate masterWillRebind:self
  421.        5:               toDelegate:anObject
  422.        6:               withExtraStuff:stuff];
  423.        7: }
  424.    Here, line 1 is assigned `objc-method-intro' syntax, and line 2 is
  425. assigned `objc-method-args-cont' syntax.  Lines 5 and 6 are both
  426. assigned `objc-method-call-cont' syntax.
  427.    Other syntactic symbols may be recognized by `cc-mode', but these
  428. are more obscure and so I haven't included examples of them.  These
  429. include: `knr-argdecl-intro', `knr-argdecl', and the `friend' modifier.
  430.    ---------- Footnotes ----------
  431.    (1)  This is the case even for C and Objective-C.  For consistency,
  432. structs in all three languages are syntactically equivalent to classes.
  433. Note however that the keyword `class' is meaningless in C and
  434. Objective-C.
  435.    (2)  The list of conditional keywords are (in C, Objective-C and
  436. C++): `for', `if', `do', `else', `while', and `switch'.  C++ has two
  437. additional conditional keywords: `try' and `catch'.
  438.    (3)  The need for this somewhat confusing arrangement is that the
  439. typical indentation desired for these lines is calculated very
  440. differently.  This should be simplified in version 5 of `cc-mode',
  441. along with the added distinction between argument lists in function
  442. declarations, and argument lists in function calls.
  443.    (4)  The `comment-intro' syntactic symbol is known generically as a
  444. "modifier" since it always appears on a syntactic analysis list with
  445. other symbols, and never has a relative buffer position.
  446.    (5)  In C++ only.
  447. File: ccmode,  Node: Performance Issues,  Next: Frequently Asked Questions,  Prev: Syntactic Symbols,  Up: Top
  448. Performance Issues
  449. ******************
  450.    C and its derivative languages are highly complex creatures.  Often,
  451. ambiguous code situations arise that require `cc-mode' to scan large
  452. portions of the buffer to determine syntactic context.  Some
  453. pathological code can cause `cc-mode' to slow down considerably.  This
  454. section identifies some of the coding styles to watch out for, and
  455. suggests some workarounds that you can use to improve performance.
  456.    Note that this is an area that will get a lot of attention in
  457. `cc-mode' version 5.  The mode should end up being much faster, at the
  458. expense of dropping Emacs 18 support, owing to the implementation of
  459. syntactic analysis caching.  This is the last release of `cc-mode' that
  460. will be compatible with Emacs 18.
  461.    Because `cc-mode' has to scan the buffer backwards from the current
  462. insertion point, and because C's syntax is fairly difficult to parse in
  463. the backwards direction, `cc-mode' often tries to find the nearest
  464. position higher up in the buffer from which to begin a forward scan.
  465. The farther this position is from the current insertion point, the
  466. slower the mode gets.  Some coding styles can even force `cc-mode' to
  467. scan from the beginning of the buffer!
  468.    One of the simplest things you can do to reduce scan time, is make
  469. sure any brace that opens a top-level block construct always appears in
  470. the leftmost column.  This is actually an Emacs constraint, as embodied
  471. in the `beginning-of-defun' function which `cc-mode' uses heavily.  If
  472. you insist on hanging top-level open braces on the right side of the
  473. line, then you should set the variable `defun-prompt-regexp' to
  474. something reasonable (1), however that "something reasonable" is
  475. difficult to define, so `cc-mode' doesn't do it for you.
  476.    You will probably notice pathological behavior from `cc-mode' when
  477. working in files containing large amounts of cpp macros.  This is
  478. because `cc-mode' cannot quickly skip backwards over these lines, which
  479. do not contribute to the syntactic calculations.  You'll probably also
  480. have problems if you are editing "K&R" C code, i.e. C code that does
  481. not use function prototypes.  This is because there are ambiguities in
  482. the C syntax when K&R style argument lists are used, and `cc-mode' has
  483. to use a slower scan to determine what it's looking at.
  484.    For the latter problem, I would suggest converting to ANSI style
  485. protocols, and turning the variable `c-recognize-knr-p' to `nil' (this
  486. is its default value for all modes).
  487.    For the former problem, you might want to investigate some of the
  488. speed-ups provided for you in the file `cc-lobotomy.el', which is part
  489. of the canonical `cc-mode' distribution.  As mentioned previously,
  490. `cc-mode' always trades accuracy for speed; however it is recognized
  491. that sometimes you need speed and can sacrifice some accuracy in
  492. indentation.  The file `cc-lobotomy.el' contains hacks that will "dumb
  493. down" `cc-mode' in some specific ways, making that trade-off of speed
  494. for accuracy.  I won't go into details of its use here; you should read
  495. the comments at the top of the file, and look at the variable
  496. `cc-lobotomy-pith-list' for details.
  497.    ---------- Footnotes ----------
  498.    (1)  Note that this variable is only defined in Emacs 19.
  499. File: ccmode,  Node: Frequently Asked Questions,  Next: Getting the latest cc-mode release,  Prev: Performance Issues,  Up: Top
  500. Frequently Asked Questions
  501. **************************
  502.      *Q.* *How do I re-indent the whole file?*
  503.      *A.* Visit the file and hit `C-x h' to mark the whole buffer. Then
  504.      hit `ESC C-\'.
  505.      *Q.* *How do I re-indent the entire function?  `ESC C-x' doesn't
  506.      work.*
  507.      *A.* `ESC C-x' is reserved for future Emacs use.  To re-indent the
  508.      entire function hit `C-c C-q'.
  509.      *Q.* *How do I re-indent the current block?*
  510.      *A.* First move to the brace which opens the block with `ESC C-u',
  511.      then re-indent that expression with `ESC C-q'.
  512.      *Q.* *Why doesn't the RET key indent the line to where the new
  513.      text should go after inserting the newline?*
  514.      *A.* Emacs' convention is that RET just adds a newline, and that
  515.      LFD adds a newline and indents it.  You can make RET do this too
  516.      by adding this to your `c-mode-common-hook' (see the sample
  517.      `.emacs' file *Note Sample .emacs File::):
  518.           (define-key c-mode-map "\C-m" 'newline-and-indent)
  519.      This is a very common question. `:-)' If you want this to be the
  520.      default behavior, don't lobby me, lobby RMS!
  521.      *Q.* *I put `(c-set-offset 'substatement-open 0)' in my `.emacs'
  522.      file but I get an error saying that `c-set-offset''s function
  523.      definition is void.*
  524.      *A.* This means that `cc-mode' wasn't loaded into your Emacs
  525.      session by the time the `c-set-offset' call was reached, mostly
  526.      likely because `cc-mode' is being autoloaded.  Instead of putting
  527.      the `c-set-offset' line in your top-level `.emacs' file, put it in
  528.      your `c-mode-common-hook', or simply add the following to the top
  529.      of your `.emacs' file:
  530.           (require 'cc-mode)
  531.      See the sample `.emacs' file *Note Sample .emacs File:: for
  532.      details.
  533.      *Q.* *How do I make strings, comments, keywords, and other
  534.      constructs appear in different colors, or in bold face, etc.?*
  535.      *A.* "Syntax Colorization" is an Emacs 19 feature, controlled by
  536.      `font-lock-mode'.  It is not part of `cc-mode'.
  537. File: ccmode,  Node: Getting the latest cc-mode release,  Next: Sample .emacs File,  Prev: Frequently Asked Questions,  Up: Top
  538. Getting the latest `cc-mode' release
  539. ************************************
  540.    `cc-mode' is now distributed with both Emacs 19 and XEmacs 19, so
  541. you would typically just use the version that comes with your Emacs.
  542. Users of older versions of Emacs can get the latest release from this
  543.          `ftp://ftp.python.org/pub/emacs/cc-mode.tar.gz'
  544.    Note that this is a "gzipped" tar file.
  545.    If you do not have anonymous ftp access, you can get the distribution
  546. through an anonymous ftp-to-mail gateway, such as the one run by DEC at
  547. `ftpmail@decwrl.dec.com'.  To get `cc-mode' via email, send the
  548. following message in the body of your mail to that address:
  549.      reply <a valid net address back to you>
  550.      connect ftp.python.org
  551.      binary
  552.      uuencode
  553.      chdir pub/emacs
  554.      get cc-mode.tar.gz
  555. or just send the message "help" for more information on ftpmail.
  556. Response times will vary with the number of requests in the queue.
  557. File: ccmode,  Node: Sample .emacs File,  Next: Requirements,  Prev: Getting the latest cc-mode release,  Up: Top
  558. Sample `.emacs' file
  559. ********************
  560.      ;; Here's a sample .emacs file that might help you along the way.  Just
  561.      ;; copy this region and paste it into your .emacs file.  You may want to
  562.      ;; change some of the actual values.
  563.      
  564.      (defconst my-c-style
  565.        '((c-tab-always-indent           . t)
  566.          (c-comment-only-line-offset    . 4)
  567.          (c-hanging-braces-alist        . ((substatement-open after)
  568.                                            (brace-list-open)))
  569.          (c-hanging-colons-alist        . ((member-init-intro before)
  570.                                            (inher-intro)
  571.                                            (case-label after)
  572.                                            (label after)
  573.                                            (access-label after)))
  574.          (c-cleanup-list                . (scope-operator
  575.                                            empty-defun-braces
  576.                                            defun-close-semi))
  577.          (c-offsets-alist               . ((arglist-close     . c-lineup-arglist)
  578.                                            (substatement-open . 0)
  579.                                            (case-label        . 4)
  580.                                            (block-open        . 0)
  581.                                            (knr-argdecl-intro . -)))
  582.          (c-echo-syntactic-information-p . t)
  583.          )
  584.        "My C Programming Style")
  585.      
  586.      ;; Customizations for all of c-mode, c++-mode, and objc-mode
  587.      (defun my-c-mode-common-hook ()
  588.        ;; add my personal style and set it for the current buffer
  589.        (c-add-style "PERSONAL" my-c-style t)
  590.        ;; offset customizations not in my-c-style
  591.        (c-set-offset 'member-init-intro '++)
  592.        ;; other customizations
  593.        (setq tab-width 8
  594.              ;; this will make sure spaces are used instead of tabs
  595.              indent-tabs-mode nil)
  596.        ;; we like auto-newline and hungry-delete
  597.        (c-toggle-auto-hungry-state 1)
  598.        ;; keybindings for C, C++, and Objective-C.  We can put these in
  599.        ;; c-mode-map because c++-mode-map and objc-mode-map inherit it
  600.        (define-key c-mode-map "\C-m" 'newline-and-indent)
  601.        )
  602.      
  603.      ;; the following only works in Emacs 19
  604.      ;; Emacs 18ers can use (setq c-mode-common-hook 'my-c-mode-common-hook)
  605.      (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
  606. File: ccmode,  Node: Requirements,  Next: Limitations and Known Bugs,  Prev: Sample .emacs File,  Up: Top
  607. Requirements
  608. ************
  609.    `cc-mode.el' requires `reporter.el' for submission of bug reports.
  610. `reporter.el' is distributed with the latest Emacs 19s.  Here is the
  611. Emacs Lisp Archive anonymous ftp'ing record for those of you who are
  612. using older Emacsen.
  613.               GNU Emacs Lisp Code Directory Apropos -- "reporter"
  614.      "~/" refers to archive.cis.ohio-state.edu:/pub/gnu/emacs/elisp-archive/
  615.      
  616.      reporter (2.12)       06-Jul-1994
  617.           Barry A. Warsaw, <bwarsaw@cnri.reston.va.us>
  618.           ~/misc/reporter.el.Z
  619.           Customizable bug reporting of lisp programs.
  620. File: ccmode,  Node: Limitations and Known Bugs,  Next: Mailing Lists and Submitting Bug Reports,  Prev: Requirements,  Up: Top
  621. Limitations and Known Bugs
  622. **************************
  623.    * Multi-line macros are not handled properly.
  624.    * Re-indenting large regions or expressions can be slow.
  625.    * Use with Emacs 18 can be slow and annoying. You should seriously
  626.      consider upgrading to Emacs 19.
  627.    * There is still some weird behavior when filling C block comments.
  628.      My suggestion is to check out add-on fill packages such as
  629.      `filladapt', available at the elisp archive.
  630.    * Lines following `inline-close' braces which hang "after" do not
  631.      line up correctly.  Hit `TAB' to reindent the line.
  632. File: ccmode,  Node: Mailing Lists and Submitting Bug Reports,  Next: Concept Index,  Prev: Limitations and Known Bugs,  Up: Top
  633. Mailing Lists and Submitting Bug Reports
  634. ****************************************
  635.    To report bugs, use the `C-c C-b' (`c-submit-bug-report') command.
  636. This provides vital information I need to reproduce your problem.  Make
  637. sure you include a concise, but complete code example.  Please try to
  638. boil your example down to just the essential code needed to reproduce
  639. the problem, and include an exact recipe of steps needed to expose the
  640. bug.  Be especially sure to include any code that appears *before* your
  641. bug example.
  642.    Bug reports are now to be sent to `bug-gnu-emacs@prep.ai.mit.edu'
  643. which is mirrored on the Usenet newsgroup `gnu.emacs.bug'.  Other
  644. questions and suggestions should be mailed to
  645. `help-gnu-emacs@prep.ai.mit.edu' which is mirrored on `gnu.emacs.help'.
  646.    Note that the `cc-mode' Majordomo mailing lists have been disbanded!
  647. With the inclusion of `cc-mode' in both of the latest flavors of Emacs
  648. 19, the need for them has ended.
  649. File: ccmode,  Node: Concept Index,  Next: Command Index,  Prev: Mailing Lists and Submitting Bug Reports,  Up: Top
  650. Concept Index
  651. *************
  652. * Menu:
  653. * -block-intro syntactic symbols:       Syntactic Symbols.
  654. * -close syntactic symbols:             Syntactic Symbols.
  655. * -cont syntactic symbols:              Syntactic Symbols.
  656. * -intro syntactic symbols:             Syntactic Symbols.
  657. * -open syntactic symbols:              Syntactic Symbols.
  658. * .emacs file:                          Getting Connected.
  659. * cc-compat.el file:                    Introduction.
  660. * cc-lobotomy.el file:                  Performance Issues.
  661. * cc-mode-18.el file:                   Getting Connected.
  662. * Adding Styles:                        Adding Styles.
  663. * Advanced Customizations:              Advanced Customizations.
  664. * announcement mailing list:            Mailing Lists and Submitting Bug Reports.
  665. * Auto-newline insertion:               Auto-newline insertion.
  666. * basic-offset (c-):                    Customizing Indentation.
  667. * beta testers mailing list:            Mailing Lists and Submitting Bug Reports.
  668. * block-close syntactic symbol:         Hanging Braces.
  669. * block-open syntactic symbol:          Hanging Braces.
  670. * BOCM:                                 Introduction.
  671. * brace lists:                          Syntactic Symbols.
  672. * brace-list-close syntactic symbol:    Hanging Braces.
  673. * brace-list-entry syntactic symbol:    Hanging Braces.
  674. * brace-list-intro syntactic symbol:    Hanging Braces.
  675. * brace-list-open syntactic symbol:     Hanging Braces.
  676. * BSD style:                            Built-in Styles.
  677. * Built-in Styles:                      Built-in Styles.
  678. * byte compile:                         Getting Connected.
  679. * c-basic-offset:                       Customizing Indentation.
  680. * c-hanging- functions:                 Indentation Commands.
  681. * c-set-offset:                         Customizing Indentation.
  682. * CC-MODE style:                        Built-in Styles.
  683. * class-close syntactic symbol:         Hanging Braces.
  684. * class-open syntactic symbol:          Hanging Braces.
  685. * Clean-ups:                            Clean-ups.
  686. * clean-ups:                            Hanging Colons.
  687. * comment only line:                    Syntactic Analysis.
  688. * comment-only line:                    Other electric commands.
  689. * Custom Brace and Colon Hanging:       Custom Brace and Colon Hanging.
  690. * custom indentation function:          Hanging Braces.
  691. * Custom Indentation Functions:         Custom Indentation Functions.
  692. * customizing brace hanging:            Custom Brace and Colon Hanging.
  693. * customizing colon hanging:            Custom Brace and Colon Hanging.
  694. * Customizing Indentation:              Customizing Indentation.
  695. * Customizing Semi-colons and Commas <1>: Other Special Indentations.
  696. * Customizing Semi-colons and Commas:   Customizing Semi-colons and Commas.
  697. * defun-close syntactic symbol:         Hanging Braces.
  698. * defun-open syntactic symbol:          Hanging Braces.
  699. * electric characters:                  Minor Modes.
  700. * electric commands:                    Auto-newline insertion.
  701. * Ellemtel style:                       Built-in Styles.
  702. * File Styles:                          File Styles.
  703. * Frequently Asked Questions:           Frequently Asked Questions.
  704. * Getting Connected:                    Getting Connected.
  705. * Getting the latest cc-mode release:   Getting the latest cc-mode release.
  706. * GNU style:                            Built-in Styles.
  707. * Hanging Braces:                       Hanging Braces.
  708. * Hanging Colons:                       Hanging Colons.
  709. * Hanging Semi-colons and commas:       Hanging Semi-colons and commas.
  710. * hooks:                                Permanent Customization.
  711. * Hungry-deletion of whitespace:        Hungry-deletion of whitespace.
  712. * in-class inline methods:              Syntactic Symbols.
  713. * Indentation Calculation:              Indentation Calculation.
  714. * Indentation Commands:                 Indentation Commands.
  715. * inline-close:                         Limitations and Known Bugs.
  716. * inline-close syntactic symbol:        Hanging Braces.
  717. * inline-open syntactic symbol:         Hanging Braces.
  718. * Interactive Customization:            Interactive Customization.
  719. * Introduction:                         Introduction.
  720. * Java style:                           Built-in Styles.
  721. * java-mode:                            Built-in Styles.
  722. * K&R style:                            Built-in Styles.
  723. * Limitations and Known Bugs:           Limitations and Known Bugs.
  724. * literal <1>:                          Indentation Commands.
  725. * literal <1>:                          Hungry-deletion of whitespace.
  726. * literal <1>:                          Clean-ups.
  727. * literal:                              Auto-newline insertion.
  728. * local variables:                      File Styles.
  729. * Mailing Lists and Submitting Bug Reports: Mailing Lists and Submitting Bug Reports.
  730. * Minor Modes:                          Minor Modes.
  731. * modifier syntactic symbol:            Syntactic Symbols.
  732. * New Indentation Engine:               New Indentation Engine.
  733. * Other electric commands:              Other electric commands.
  734. * Performance Issues:                   Performance Issues.
  735. * Permanent Indentation:                Permanent Customization.
  736. * relative buffer position:             Syntactic Analysis.
  737. * reporter.el:                          Requirements.
  738. * Requirements:                         Requirements.
  739. * Sample .emacs file:                   Sample .emacs File.
  740. * set-offset (c-):                      Customizing Indentation.
  741. * statement-case-open syntactic symbol: Hanging Braces.
  742. * stream-op syntactic symbol:           Custom Indentation Functions.
  743. * Stroustrup style:                     Built-in Styles.
  744. * Styles:                               Styles.
  745. * substatement:                         Syntactic Analysis.
  746. * substatement block:                   Syntactic Analysis.
  747. * substatement-open syntactic symbol:   Hanging Braces.
  748. * Syntactic Analysis:                   Syntactic Analysis.
  749. * syntactic component:                  Syntactic Analysis.
  750. * syntactic component list:             Syntactic Analysis.
  751. * syntactic symbol:                     Syntactic Analysis.
  752. * Syntactic Symbols:                    Syntactic Symbols.
  753. * syntactic whitespace <1>:             Syntactic Symbols.
  754. * syntactic whitespace:                 Auto-newline insertion.
  755. * TAB:                                  Indentation Calculation.
  756. * Whitesmith style:                     Built-in Styles.
  757. File: ccmode,  Node: Command Index,  Next: Key Index,  Prev: Concept Index,  Up: Top
  758. Command Index
  759. *************
  760.    Since all `cc-mode' commands are prepended with the string `c-',
  761. each appears under its `c-<thing>' name and its `<thing> (c-)' name.
  762. * Menu:
  763. * add-style (c-):                       Adding Styles.
  764. * beginning-of-defun:                   Performance Issues.
  765. * c-add-style:                          Adding Styles.
  766. * c-electric-brace:                     Hanging Braces.
  767. * c-electric-delete:                    Hungry-deletion of whitespace.
  768. * c-electric-pound:                     Other electric commands.
  769. * c-electric-slash:                     Other electric commands.
  770. * c-electric-star:                      Other electric commands.
  771. * c-hanging-braces-alist:               Indentation Commands.
  772. * c-indent-command:                     Indentation Commands.
  773. * c-indent-defun <1>:                   Interactive Customization.
  774. * c-indent-defun:                       Indentation Commands.
  775. * c-indent-exp:                         Indentation Commands.
  776. * c-lineup-streamop:                    Custom Indentation Functions.
  777. * c-mark-function:                      Indentation Commands.
  778. * c-set-offset <1>:                     File Styles.
  779. * c-set-offset:                         Interactive Customization.
  780. * c-set-style <1>:                      Built-in Styles.
  781. * c-set-style:                          Indentation Commands.
  782. * c-show-syntactic-information:         Syntactic Analysis.
  783. * c-snug-do-while:                      Custom Brace and Colon Hanging.
  784. * c-submit-bug-report:                  Mailing Lists and Submitting Bug Reports.
  785. * c-toggle-auto-hungry-state:           Minor Modes.
  786. * c-toggle-auto-state:                  Minor Modes.
  787. * c-toggle-hungry-state:                Minor Modes.
  788. * c-version:                            Introduction.
  789. * defun-prompt-regexp:                  Performance Issues.
  790. * electric-brace (c-):                  Hanging Braces.
  791. * electric-delete (c-):                 Hungry-deletion of whitespace.
  792. * electric-pound (c-):                  Other electric commands.
  793. * electric-slash (c-):                  Other electric commands.
  794. * electric-star (c-):                   Other electric commands.
  795. * hanging-braces-alist (c-):            Indentation Commands.
  796. * indent-command (c-):                  Indentation Commands.
  797. * indent-defun (c-) <1>:                Interactive Customization.
  798. * indent-defun (c-):                    Indentation Commands.
  799. * indent-exp (c-):                      Indentation Commands.
  800. * indent-region:                        Indentation Commands.
  801. * lineup-streamop (c-):                 Custom Indentation Functions.
  802. * mark-function (c-):                   Indentation Commands.
  803. * newline-and-indent:                   Frequently Asked Questions.
  804. * set-offset (c-) <1>:                  File Styles.
  805. * set-offset (c-):                      Interactive Customization.
  806. * set-style (c-) <1>:                   Built-in Styles.
  807. * set-style (c-):                       Indentation Commands.
  808. * show-syntactic-information (c-):      Syntactic Analysis.
  809. * snug-do-while (c-):                   Custom Brace and Colon Hanging.
  810. * submit-bug-report (c-):               Mailing Lists and Submitting Bug Reports.
  811. * toggle-auto-hungry-state (c-):        Minor Modes.
  812. * toggle-auto-state (c-):               Minor Modes.
  813. * toggle-hungry-state (c-):             Minor Modes.
  814. File: ccmode,  Node: Key Index,  Next: Variable Index,  Prev: Command Index,  Up: Top
  815. Key Index
  816. *********
  817. * Menu:
  818. * #:                                    Other electric commands.
  819. * C-c C-a:                              Minor Modes.
  820. * C-c C-b:                              Mailing Lists and Submitting Bug Reports.
  821. * C-c C-d:                              Minor Modes.
  822. * C-c C-o:                              Interactive Customization.
  823. * C-c C-q <1>:                          Frequently Asked Questions.
  824. * C-c C-q <1>:                          Custom Indentation Functions.
  825. * C-c C-q <1>:                          Interactive Customization.
  826. * C-c C-q:                              Indentation Commands.
  827. * C-c C-s <1>:                          Syntactic Symbols.
  828. * C-c C-s:                              Syntactic Analysis.
  829. * C-c C-t:                              Minor Modes.
  830. * C-u:                                  Auto-newline insertion.
  831. * C-x h:                                Frequently Asked Questions.
  832. * DEL:                                  Hungry-deletion of whitespace.
  833. * ESC C-\:                              Frequently Asked Questions.
  834. * ESC C-q:                              Frequently Asked Questions.
  835. * ESC C-u:                              Frequently Asked Questions.
  836. * ESC C-x:                              Frequently Asked Questions.
  837. * LFD:                                  Frequently Asked Questions.
  838. * M-C-\:                                Indentation Commands.
  839. * M-C-h:                                Indentation Commands.
  840. * M-C-q:                                Indentation Commands.
  841. * RET:                                  Frequently Asked Questions.
  842. * TAB <1>:                              Limitations and Known Bugs.
  843. * TAB <1>:                              Indentation Commands.
  844. * TAB:                                  Indentation Calculation.
  845. File: ccmode,  Node: Variable Index,  Prev: Key Index,  Up: Top
  846. Variable Index
  847. **************
  848.    Since all `cc-mode' variables are prepended with the string `c-',
  849. each appears under its `c-<thing>' name and its `<thing> (c-)' name.
  850. * Menu:
  851. * basic-offset (c-):                    Advanced Customizations.
  852. * c++-mode-hook:                        Permanent Customization.
  853. * c-basic-offset:                       Advanced Customizations.
  854. * c-cleanup-list:                       Clean-ups.
  855. * c-delete-function:                    Hungry-deletion of whitespace.
  856. * c-echo-syntactic-information-p:       Indentation Calculation.
  857. * c-electric-pound-behavior:            Other electric commands.
  858. * c-file-offsets:                       File Styles.
  859. * c-file-style:                         File Styles.
  860. * c-hanging-braces-alist <1>:           Custom Brace and Colon Hanging.
  861. * c-hanging-braces-alist:               Hanging Braces.
  862. * c-hanging-colon-alist:                Custom Brace and Colon Hanging.
  863. * c-hanging-colons-alist:               Hanging Colons.
  864. * c-hanging-semi&comma-criteria:        Customizing Semi-colons and Commas.
  865. * c-mode-common-hook:                   Permanent Customization.
  866. * c-mode-hook:                          Permanent Customization.
  867. * c-offsets-alist <1>:                  Syntactic Symbols.
  868. * c-offsets-alist <1>:                  Custom Indentation Functions.
  869. * c-offsets-alist <1>:                  File Styles.
  870. * c-offsets-alist <1>:                  Other electric commands.
  871. * c-offsets-alist <1>:                  Hanging Braces.
  872. * c-offsets-alist <1>:                  Indentation Calculation.
  873. * c-offsets-alist:                      Syntactic Analysis.
  874. * c-progress-interval:                  Indentation Commands.
  875. * c-recognize-knr-p:                    Performance Issues.
  876. * c-special-indent-hook:                Other Special Indentations.
  877. * c-style-alist <1>:                    Advanced Customizations.
  878. * c-style-alist:                        Adding Styles.
  879. * c-syntactic-context:                  Custom Brace and Colon Hanging.
  880. * c-tab-always-indent:                  Indentation Commands.
  881. * cc-lobotomy-pith-list:                Performance Issues.
  882. * cleanup-list (c-):                    Clean-ups.
  883. * delete-function (c-):                 Hungry-deletion of whitespace.
  884. * echo-syntactic-information-p (c-):    Indentation Calculation.
  885. * electric-pound-behavior (c-):         Other electric commands.
  886. * file-offsets (c-):                    File Styles.
  887. * file-style (c-):                      File Styles.
  888. * hanging-braces-alist (c-) <1>:        Custom Brace and Colon Hanging.
  889. * hanging-braces-alist (c-):            Hanging Braces.
  890. * hanging-colon-alist (c-):             Custom Brace and Colon Hanging.
  891. * hanging-colons-alist (c-):            Hanging Colons.
  892. * hanging-semi&comma-criteria (c-):     Customizing Semi-colons and Commas.
  893. * java-mode-hook:                       Permanent Customization.
  894. * objc-mode-hook:                       Permanent Customization.
  895. * offsets-alist (c-) <1>:               Syntactic Symbols.
  896. * offsets-alist (c-) <1>:               Custom Indentation Functions.
  897. * offsets-alist (c-) <1>:               File Styles.
  898. * offsets-alist (c-) <1>:               Other electric commands.
  899. * offsets-alist (c-) <1>:               Hanging Braces.
  900. * offsets-alist (c-) <1>:               Indentation Calculation.
  901. * offsets-alist (c-):                   Syntactic Analysis.
  902. * progress-interval (c-):               Indentation Commands.
  903. * recognize-knr-p (c-):                 Performance Issues.
  904. * special-indent-hook (c-):             Other Special Indentations.
  905. * style-alist (c-) <1>:                 Advanced Customizations.
  906. * style-alist (c-):                     Adding Styles.
  907. * syntactic-context (c-):               Custom Brace and Colon Hanging.
  908. * tab-always-indent (c-):               Indentation Commands.
  909.